package com.lu.manage.modular.system.controller;

import com.lu.manage.core.base.controller.BaseController;
import com.lu.manage.core.common.exception.SysLogExcepetion;
import com.lu.manage.core.common.node.ZTreeNode;
import com.lu.manage.core.tips.SuccessTip;
import com.lu.manage.modular.system.model.DictType;
import com.lu.manage.modular.system.service.DictTypeService;
import com.lu.manage.modular.system.service.SystemCacheService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Project LuBoot
 * @Author: zhanglu
 * @Date: 2019-11-17 00:46:19
 * @Description: 字典类型表 控制器
 */
@Controller
@RequestMapping("/system/dictType")
public class DictTypeController extends BaseController {

    private static final String PREFIX = "/modular/system/dictType";

    @Autowired
    private DictTypeService dictTypeService;

    @Autowired
    private SystemCacheService systemCacheService;


/*******************************跳转页面相关*******************************/

    /**
    * 首页
    * @return
    */
    @GetMapping("")
    public String index(){
        return PREFIX + "/index.html";
    }

    /**
    * 新增
    * @return
    */
    @GetMapping("/toAdd")
    public String toAdd() {
        return PREFIX + "/add.html";
    }

    /**
    * 修改
    * @return
    */
    @GetMapping("/toUpdate")
    public String toUpdate(String id, Model model) throws SysLogExcepetion {
        DictType obj = dictTypeService.getById(id);
        model.addAttribute("obj", obj);
        return PREFIX + "/update.html";
    }

/*******************************操作按钮相关*******************************/

    /**
     * 添加
     */
    @PostMapping("/add")
    @ResponseBody
    @RequiresPermissions(value = "/system/dictType/add")
    public Object add(DictType dictType) throws SysLogExcepetion {
        dictTypeService.addObj(dictType);
        return new SuccessTip();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ResponseBody
    @RequiresPermissions(value = "/system/dictType/update")
    public Object update(DictType dictType) throws SysLogExcepetion {
        dictTypeService.updateObj(dictType);
        return new SuccessTip();
    }

    /**
     * 删除&批量
     */
    @PostMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = "/system/dictType/delete")
    public Object delete(String data) throws SysLogExcepetion {
        dictTypeService.deleteObj(data);
        return new SuccessTip();
    }

    /**
     * 排序
     * @param nodes
     * @return
     * @throws Exception
     */
    @PostMapping("/sort")
    @ResponseBody
    @RequiresPermissions(value = "/system/dictType/sort")
    public Object sort(String nodes) throws Exception{
        this.dictTypeService.sortAllDictTypes(nodes);
        return new SuccessTip();
    }

    /**
     * 修改状态
     *  - 修改本表
     *  - 修改与之关联的子表
     */
    @PostMapping("/updateState")
    @ResponseBody
    @RequiresPermissions(value = "/system/dictType/updateState")
    public Object updateState(String id, Integer state) throws SysLogExcepetion {
        dictTypeService.updateState(id, state);
        return new SuccessTip();
    }

    /**
     * 更新缓存
     *  - 子表 字典
     */
    @PostMapping("/dictTypeCache")
    @ResponseBody
    @RequiresPermissions(value = "/system/dictType/dictTypeCache")
    public Object dictTypeCache(String dictTypeIds) throws SysLogExcepetion {
        systemCacheService.dictTypeCache(dictTypeIds);
        return new SuccessTip();
    }

    /**
     * 更新缓存
     *  - 子表 字典
     */
    @PostMapping("/dictCache")
    @ResponseBody
    @RequiresPermissions(value = "/system/dictType/dictCache")
    public Object dictCache(String dictTypeId, String dictIds) throws SysLogExcepetion {
        systemCacheService.dictCache(dictTypeId, dictIds);
        return new SuccessTip();
    }


/*******************************查询数据相关*******************************/

    /**
    * 分页查询
    */
    @GetMapping("/listPage")
    @ResponseBody
    public Object listPage(@RequestParam Map<String, Object> map) throws SysLogExcepetion {
        return dictTypeService.listPage(map);
    }

    /**
     * 字典类型树
     * @return
     */
    @GetMapping("/dictTypeTreeList")
    @ResponseBody
    public Object roleTreeList(){
        List<ZTreeNode> roleTreeList = (List<ZTreeNode>) this.dictTypeService.dictTypeTreeList();
        roleTreeList.add(ZTreeNode.createParent());
        return roleTreeList;
    }


}
